package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.Efficiency;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

@Mapper
public interface EfficiencyMapper extends BaseMapper<Efficiency> {

    //查询里程
    @Select("SELECT IFNULL(SUM(total_distance),0) FROM efficiency WHERE staff_id = #{id} AND belong_time BETWEEN #{startTime} and #{endTime} ")
    int findTotalDistance(@Param("id") int id,
                          @Param("startTime") String startTime,
                          @Param("endTime") String endTime);

    //时长
    @Select("SELECT IFNULL(SUM(duration),0) FROM efficiency WHERE staff_id = #{id} AND belong_time BETWEEN #{startTime} and #{endTime} ")
    int findDuration(@Param("id") int id,
                     @Param("startTime") String startTime,
                     @Param("endTime") String endTime);

    //活跃度
    @Select("SELECT IFNULL(SUM(liveness),0) FROM efficiency WHERE staff_id = #{id} AND belong_time BETWEEN #{startTime} and #{endTime} ")
    int findLiveness(@Param("id") int id,
                     @Param("startTime") String startTime,
                     @Param("endTime") String endTime);

    //违法数
    @Select("SELECT IFNULL(SUM(illegal_data),0) FROM efficiency WHERE staff_id = #{id} AND belong_time BETWEEN #{startTime} and #{endTime} ")
    int findIllegalData(@Param("id") int id,
                        @Param("startTime") String startTime,
                        @Param("endTime") String endTime);

    //接处警数
    @Select("SELECT IFNULL(SUM(Jie_chuJing),0) FROM efficiency WHERE staff_id = #{id} AND belong_time BETWEEN #{startTime} and #{endTime} ")
    int findJieChuJing(@Param("id") int id,
                       @Param("startTime") String startTime,
                       @Param("endTime") String endTime);

    @Select("SELECT IFNULL(round(SUM(${param1}),0),0) as avgNum,COUNT(*) as count FROM efficiency e LEFT JOIN staff s ON e.staff_id = s.sys_staff_id LEFT JOIN section sc ON s.sys_section_id = sc.sys_section_id \n" +
            " WHERE sc.section_pid = #{param2} AND belong_time BETWEEN #{param3} and #{param4} ")
    Map findMean(String field, int id, String startTime, String endTime);


    int insertIgnoreNull(Efficiency efficiency);


}
